package com.brkj.search;

import java.lang.reflect.Type;
import java.util.List;

import com.brkj.httpInterface.HttpInterface;
import com.brkj.search.model.DS_SearchResult;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import com.brkj.httpInterface.BaseAjaxParams;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.brkj.course.CourseAdapter;
import com.brkj.model.DS_Course;
import com.brkj.main.R;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.httpInterface.HttpInterface.HIF_GetCourseByTag;
import com.brkj.util.view.BaseActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class SearchResultActivity extends BaseActivity{

	@ViewInject(id=R.id.resultText) TextView resultText;
	@ViewInject(id=R.id.courseListView) ListView courseListView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.search_result);
		setActivityTitle("搜索结果");
		setReturnBtn();
		
		getCourseByTag();
		
	}

	
	public void getCourseByTag(){
		
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HttpInterface.HIF_SearchKnowledgeBase.params.tag, getIntent().getStringExtra("key"));

		FinalHttp fh = new FinalHttp();
		fh.post(HttpInterface.HIF_SearchKnowledgeBase.address, params, new MyAjaxCallBack<Object>(this) {

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				
				Gson gson = new Gson();
				Type listType = new TypeToken<List<DS_SearchResult>>(){}.getType();
				List<DS_SearchResult> resultList = gson.fromJson((String)t, listType);
		
				if(resultList != null){
					resultText.setText("共搜索到" + resultList.size() + "条相关知识！");
					
					SearchResultAdapter adapter =
							new SearchResultAdapter(SearchResultActivity.this, resultList);
					courseListView.setAdapter(adapter);
				}else{
					resultText.setText("没有搜索到相关知识！");
				}

			}
			
		});
	}
	
}
